package com.yyf.springboot2.common;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;


public class HttpUtil {
	
	private static boolean showurl = true;
	
	public static String sendGet(String url, String param) {
		String result = "";
		BufferedReader in = null;
		try {
			String urlNameString = url + "?" + param;
			if (param==null||param==""){
				urlNameString = url;
			}
			if (showurl){
				System.out.println(urlNameString);
			}
			URL realUrl = new URL(urlNameString);
			// 打开和URL之间的连接
			URLConnection connection = realUrl.openConnection();
			// 设置通用的请求属性
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			// 建立实际的连接
			connection.connect();
			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			LoggerUtil.error(e);
		}
		finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
				LoggerUtil.error(e2);
			}
		}
		return result;
	}
	
	public static String sendGetWithOutParam(String url) {
		String result = "";
		BufferedReader in = null;
		try {
			if (showurl){
				System.out.println(url);
			}
			URL realUrl = new URL(url);
			// 打开和URL之间的连接
			URLConnection connection = realUrl.openConnection();
			// 设置通用的请求属性
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			// 建立实际的连接
			connection.connect();
			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			LoggerUtil.error(e);
		}
		finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
				LoggerUtil.error(e2);
			}
		}
		return result;
	}
	
	public static int getRespStatus(String url){
			URL obj;
			try {
				obj = new URL(url);
				HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
				int status = conn.getResponseCode();
				return status;
			} catch (Exception e) {
				return 0;
			}
	}
}